Main site

Built-in Modules > Myfastway Class

Myfastway Class

Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.

Overview

The Myfastway class enables seamless integration with Myfastway courier services to fetch real-time shipping rates.

Class Definition

class Myfastway {
  constructor(myfastway_client_id, myfastway_client_secret, destination) {
    // Class constructor
  }

  async getRates(parcels) {
    // Method to fetch rates
  }
}

Arguments

  • myfastway_client_id (String): The client ID associated with your Myfastway account.
  • myfastway_client_secret (String): The client secret for your Myfastway account.
  • destination (Object): The "destination" information from the DATA JSON object provided to the calculateShippingRates function.

Usage Example

import { Myfastway } from "./modules.js"
const myfastway = new Myfastway(env.myfastway_client_id, env.myfastway_client_secret, DATA.destination);
const parcels = [
  { length: 104, width: 30, height: 10, weight: 20, quantity: 1 },
  // ... additional parcels
];
const rates = await myfastway.getRates(parcels);

This usage example demonstrates how to initiate the Myfastway class with the necessary account credentials and destination details. The getRates method computes shipping rates for a series of parcels, each defined by their dimensions (in cm) and weight (in kg). The format of the returned rates aligns with the shipping rates format used by Shopify. The plug-in is valid for parcels, not satchels. For better security, your Myfastway credentials should be saved in the env object as encrypted secret variables in the JsRates Settings page.

Sample AI Prompt

I'd like a shipping rate calculator that uses the Myfastway rates.
The dimensions of the parcels are contained in the items metafield namespace object called "carton". Get the first 15 values in the namespace.
The client id and client secret are saved in the environment variable.

Sample AI Output

import { enrichItemDetails } from './modules.js';
import { Myfastway } from './modules.js';

export async function calculateShippingRates(DATA, env) {
    try {
        // Enrich item details with 'carton' metafield
        DATA = await enrichItemDetails(DATA, [{ namespace: "carton", size: 15 }]);

        // Prepare parcels array for Myfastway
        const parcels = DATA.items.map(item => {
            const cartonMetafield = item.metafields && item.metafields.carton;
            return {
                length: cartonMetafield ? parseInt(cartonMetafield.length) : 0,
                width: cartonMetafield ? parseInt(cartonMetafield.width) : 0,
                height: cartonMetafield ? parseInt(cartonMetafield.height) : 0,
                weight: item.grams / 1000, // Convert grams to kg
                quantity: item.quantity
            };
        }).filter(parcel => parcel.length > 0 && parcel.width > 0 && parcel.height > 0);

        // Get rates from Myfastway using environment variables for authentication
        const myfastway = new Myfastway(env.myfastway_client_id, env.myfastway_client_secret, DATA.destination);
        const rates = await myfastway.getRates(parcels);

        return { rates };
    } catch (error) {
        // Return a rate with price equals to null in case of errors
        return { rates: [{ service_name: "", service_code: "", total_price: null, description: "", currency: "", min_delivery_date: "", max_delivery_date: "" }] };
    }
}